/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: ws
 * Date: 2023-02-19
 * Time: 12:03
 */
public class BinarySearch {
    public TreeNode root;

    static class TreeNode {
        public int key;
        public TreeNode left;
        public TreeNode right;

        TreeNode(int key) {
            this.key = key;
        }
    }


    /**
     * 插入一个元素
     * @param key
     */
    public boolean insert(int key) {
        if (root == null) {
            root = new TreeNode(key);
            return true;
        }
        TreeNode cur = root;
        TreeNode parent = null;//记录插入位置的父节点
        while (cur != null) {//当cur为null时就找到插入的位置
            if (key < cur.key) {
                parent = cur;
                cur = cur.left;
            } else if (key > cur.key){
                parent = cur;
                cur = cur.right;
            } else {//该key已经存在
                return false;
            }
        }
        //此时的cur为null，将节点插入到parent的left或right
        TreeNode node = new TreeNode(key);
        if (key < parent.key) {
            parent.left = node;
        } else {
            parent.right = node;
        }
        return true;
    }
    //查找key是否存在
    public TreeNode search(int key) {
        TreeNode curr = root;
        while (curr != null) {
            if (key == curr.key) {
                return curr;
            } else if (key < curr.key) {
                curr = curr.left;
            } else {
                curr = curr.right;
            }
        }
        return null;
    }
    //删除key的值
    public boolean remove(int key) {
        TreeNode curr = root;
        TreeNode parent = null;
        while (curr != null) {
            if (key == curr.key) {
                removeNode(curr, parent);
                return true;
            } else if (key < curr.key) {
                parent = curr;
                curr = curr.left;
            } else {
                parent = curr;
                curr = curr.right;
            }
        }
        return false;
    }

    public void removeNode(TreeNode curr, TreeNode parent) {
        if (curr.left == null) {
            if (curr == root) {
                root = root.right;
            } else if (curr == parent.left) {
                parent.left = curr.right;
            } else {
                parent.right = curr.right;
            }
        } else if (curr.right == null) {
            if (curr == root) {
                root = root.left;
            } else if (curr == parent.left) {
                parent.left = curr.left;
            } else {
                parent.right = curr.left;
            }
        } else {//左右都不为null，找到right的左子树的最小值替换
            TreeNode targetParent = curr;
            TreeNode target = curr.right;
            while (target.left != null) {
                targetParent = target;
                target = target.left;
            }
            curr.key = target.key;
            if (target == targetParent.left) {
                targetParent.left = target.right;//可能target不是叶子节点
            } else {
                targetParent.right = target.right;//target==curr.right
            }
        }
    }
}
